/*
 * PanelSusan.java
 *
 * Created on 2008年4月12日, 下午12:37
 */

package GUI;

import Global.Global;
import Manager.Manager;

/**
 *
 * @author  littlechild
 */
public class PanelSusan extends javax.swing.JPanel {
    Manager manager;
    PanelCavas drawcavas;
    boolean isInitiating;
    static int maxR=7;
    static int minR=0;
    static int maxT=200;
    static int minT=0;
    static int maxRatio=100;
    static int minRatio=0;
    static double scaleRatio=100;
    /** Creates new form PanelSusan */
    public PanelSusan(Manager mg,PanelCavas pc) {
        manager=mg;
        drawcavas=pc;
        initComponents();
        init();
    }

    private void init() {
         isInitiating=true;
         this.jSliderT.setMaximum(maxT);
         this.jSliderT.setMinimum(minT);
         this.jSliderR.setMaximum(maxR);
         this.jSliderR.setMinimum(minR);
         this.jSliderRatio.setMaximum(maxRatio);
         this.jSliderRatio.setMinimum(minRatio);
         this.jSliderT.setValue(23);
         this.jSliderR.setValue(3);
         this.jSliderRatio.setValue(75);
         isInitiating=false;
         //preview();
    }

    private void preview() {
        if(!isInitiating) {
            //this.jLabelStatus.setText("processing...");
            String paras[]=new String [] {
              String.valueOf(this.jSliderT.getValue()),
              String.valueOf(this.jSliderR.getValue()),
              String.valueOf(sliderRatio2realRatio(this.jSliderRatio.getValue()))
            };
            this.drawcavas.setImage(
                    manager.operate(Global.opSusan, paras,true));
            //this.jLabelStatus.setText("OK");
            javax.swing.JOptionPane.showMessageDialog(this, "操作完成","操作完成",javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private double sliderRatio2realRatio(int a) {
        return a/scaleRatio;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSliderT = new javax.swing.JSlider();
        jSliderR = new javax.swing.JSlider();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabelT = new javax.swing.JLabel();
        jLabelR = new javax.swing.JLabel();
        jButtonOk1 = new javax.swing.JButton();
        jButtonCancel1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jSliderRatio = new javax.swing.JSlider();
        jLabelRatio = new javax.swing.JLabel();
        jLabelStatus = new javax.swing.JLabel();

        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                formComponentAdded(evt);
            }
        });

        jSliderT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderTMouseReleased(evt);
            }
        });
        jSliderT.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderTStateChanged(evt);
            }
        });

        jSliderR.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderRMouseReleased(evt);
            }
        });
        jSliderR.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderRStateChanged(evt);
            }
        });

        jLabel4.setText("灰度差别阈值：");

        jLabel5.setText("圆形领域半径：");

        jLabelT.setText("adsfa");

        jLabelR.setText("adsfasdf");

        jButtonOk1.setText("确定");
        jButtonOk1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOk1ActionPerformed(evt);
            }
        });

        jButtonCancel1.setText("取消");
        jButtonCancel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancel1ActionPerformed(evt);
            }
        });

        jLabel6.setText("差别比例阈值：");

        jSliderRatio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSliderRatioMouseReleased(evt);
            }
        });
        jSliderRatio.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderRatioStateChanged(evt);
            }
        });

        jLabelRatio.setText("adfaf");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(181, Short.MAX_VALUE)
                .addComponent(jLabelR)
                .addGap(177, 177, 177))
            .addGroup(layout.createSequentialGroup()
                .addGap(138, 138, 138)
                .addComponent(jLabelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(127, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(100, 100, 100)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSliderT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel4)
                                .addComponent(jLabel6))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabelT)
                                .addComponent(jLabelRatio)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButtonOk1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonCancel1))
                            .addComponent(jSliderR, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jSliderRatio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(100, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(115, 115, 115)
                .addComponent(jLabelR, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(141, 141, 141)
                .addComponent(jLabelStatus)
                .addContainerGap(67, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(67, 67, 67)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jLabelT))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSliderT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSliderR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jLabelRatio))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSliderRatio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(11, 11, 11)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonOk1)
                        .addComponent(jButtonCancel1))
                    .addContainerGap(42, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_formComponentAdded

    private void jSliderTStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderTStateChanged
        // TODO add your handling code here:
        this.jLabelT.setText(String.valueOf(this.jSliderT.getValue()));
}//GEN-LAST:event_jSliderTStateChanged

    private void jSliderRStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderRStateChanged
        // TODO add your handling code here:
        this.jLabelR.setText(String.valueOf(this.jSliderR.getValue()));
}//GEN-LAST:event_jSliderRStateChanged

    private void jButtonOk1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOk1ActionPerformed
        // TODO add your handling code here:
        String paras[]=new String [] {
          String.valueOf(this.jSliderT.getValue()),
          String.valueOf(this.jSliderR.getValue()),
          String.valueOf(sliderRatio2realRatio(this.jSliderRatio.getValue()))
        };
        this.drawcavas.setImage(
                manager.operate(Global.opSusan, paras));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButtonOk1ActionPerformed

    private void jButtonCancel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancel1ActionPerformed
        // TODO add your handling code here:
        this.drawcavas.setImage(
                manager.operate(Global.opNone, null));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButtonCancel1ActionPerformed

    private void jSliderRatioStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderRatioStateChanged
        // TODO add your handling code here:
        this.jLabelRatio.setText(
                String.valueOf(sliderRatio2realRatio(this.jSliderRatio.getValue())));
}//GEN-LAST:event_jSliderRatioStateChanged

    private void jSliderRatioMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderRatioMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderRatioMouseReleased

    private void jSliderRMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderRMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderRMouseReleased

    private void jSliderTMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSliderTMouseReleased
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jSliderTMouseReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel1;
    private javax.swing.JButton jButtonOk1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelR;
    private javax.swing.JLabel jLabelRatio;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JLabel jLabelT;
    private javax.swing.JSlider jSliderR;
    private javax.swing.JSlider jSliderRatio;
    private javax.swing.JSlider jSliderT;
    // End of variables declaration//GEN-END:variables
    
}
